package com.htgd.gardenexpo.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeConIntegralGoodsDTO;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.page.GoodsQuery;
import com.htgd.gardenexpo.service.GeBaseDictItemService;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeConIntegralGoodsService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConIntegralGoodsVO;
import com.htgd.gardenexpo.vo.GeConPlantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author: 王秀田
 * @date: 2024/11/13 17:08
 * @description: 积分商品 前端控制器
 */
@RestController
@RequestMapping("/api/integralGoods")
@Api(tags = "积分商品")
public class GeConIntegralGoodsController {

    @Autowired
    private GeConIntegralGoodsService geConIntegralGoodsService;

    @Autowired
    private GeBaseDictItemService geBaseDictItemService;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @GetMapping(value = "/goodsTypeList")
    @ApiOperation("查询商品类型")
    public CommonResult<List<GeBaseDictItem>> goodsTypeList() {
        CommonResult<List<GeBaseDictItem>> result =new CommonResult<>();
        List<GeBaseDictItem> items = geBaseDictItemService.getGoodsType();
        if(CollUtil.isNotEmpty(items)){
            result.setCode(200);
            result.setData(items);
        }else {
            result.setCode(300);
            result.setMessage("字典code不存在");
        }
        return result;
    }

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConIntegralGoodsDTO geConIntegralGoods) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralGoodsService.add(geConIntegralGoods);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    @Transactional
    public CommonResult<JSONObject> edit(@RequestBody GeConIntegralGoodsDTO geConIntegralGoods) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeConIntegralGoods goods = new GeConIntegralGoods();
            BeanUtils.copyProperties(geConIntegralGoods,goods);
            goods.setStatus("0");
            goods.setUpdateTime(LocalDateTime.now());
            List<String> images = geConIntegralGoods.getImages();
            if(!images.isEmpty()){
                goods.setPicture(JSON.toJSON(images).toString());
            }
            geConIntegralGoodsService.updateById(goods);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    @Transactional
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeConIntegralGoods goods = new GeConIntegralGoods();
            goods.setDeleteFlag("1");
            goods.setId(id);
            goods.setDeleteTime(LocalDateTime.now());
            geConIntegralGoodsService.updateById(goods);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConIntegralGoods>> list(GoodsQuery goodsQuery) {
        CommonResult<List<GeConIntegralGoods>> result = new CommonResult<>();
        try {
            PageInfo<GeConIntegralGoods> list = geConIntegralGoodsService.pageList(goodsQuery);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    @Transactional
    public CommonResult<GeConIntegralGoods> details(String id) {
        CommonResult<GeConIntegralGoods> result = new CommonResult<>();
        try {
            GeConIntegralGoods goods = geConIntegralGoodsService.getById(id);
            goods.setImages(JSONArray.parseArray(goods.getPicture(),String.class));
            goods.setTypeText(geBaseDictService.getGoodsType(goods.getType()));
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(goods);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/review")
    @ApiOperation("上架/下架")
    public CommonResult<JSONObject> review(@RequestBody GeConIntegralGoods goods) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if(Objects.equals(goods.getStatus(),"0")){
                goods.setLowerTime(LocalDateTime.now());
                geConIntegralGoodsService.updateById(goods);
                result.setCode(200);
                result.setMessage("下架成功");
            }else{
                goods.setUpperTime(LocalDateTime.now());
                geConIntegralGoodsService.updateById(goods);
                result.setCode(200);
                result.setMessage("上架成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("操作失败");
        }
        return result;
    }

    @PostMapping(value = "/prepareSubmit")
    @ApiOperation("积分商品预提交")
    public CommonResult<GeConIntegralGoodsVO> prepareSubmit(@RequestBody GeConIntegralGoods goods, HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        return geConIntegralGoodsService.prepareSubmit(goods.getId(),goods.getNum(),openId);
    }

    @PostMapping(value = "/goodsSubmit")
    @ApiOperation("积分商品确定支付")
    public CommonResult<GeConIntegralGoodsOrder> goodsSubmit(@RequestBody GeConIntegralGoods goods, HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        return geConIntegralGoodsService.goodsSubmit(goods,openId);
    }


}
